package ca.uqac.dim.turtledb.physical;

import ca.uqac.dim.turtledb.*;

/**
 * User: damien Date: 12-12-02 Time: 02:02
 * <p/>
 * Composé d'un Table et presente l'implémentation de celle-ci pour son
 * interface Relation La différence c'est qu'elle connait le Site Hote et
 * qu'elle dit oui quand on lui demande si elle est un fragment
 */
public class Fragment extends Table {

    private Condition condition;
    private Projection schemaFrag;
    //Reference pour qu'un fragment puisse retrouver son site hote
    private Site host;

    public Fragment(Table table, Condition condition) {
        super(table);
        this.condition = condition;
    }

    public Fragment(String name, Table table, Condition condition) {
        super(table);
        this.condition = condition;
        this.setName(name);
    }

    public Fragment(Table table, Projection condition) {
        super(table);
        this.schemaFrag = condition;
    }

    public Fragment(String name, Table table, Projection projection) {
        super(table);
        this.schemaFrag = projection;
        this.setName(name);
    }

    /**
     * Constructor by copy
     *
     * @param r
     */
    public Fragment(Relation r) {
        super(r);
    }

    public Site getHost() {
        return host;
    }

    void setHost(Site host) {
        this.host = host;
    }

    public Condition getCondition() {
        return condition;
    }

    public void setCondition(Condition condition) {
        this.condition = condition;
    }

    public Projection getSchemaFrag() {
        return schemaFrag;
    }

    public void setSchemaFrag(Projection schemaFrag) {
        this.schemaFrag = schemaFrag;
    }

    @Override
    public boolean isFragment() {
        return true;
    }

    public Boolean isHorizontallyFragmented() {
        if (condition == null) {
            return false;
        } else {
            return true;
        }
    }

    public boolean isVerticallyFragmented() {
        if (schemaFrag == null) {
            return false;
        } else {
            return true;
        }
    }

    public VariableTable toVariableTable() {
        return new VariableTable(getName(), getHost().getName());
    }
}
